package com.test.template.publish.using.elecbook.test;

import com.test.template.publish.config.*;
import com.test.template.publish.config.fac.*;
import com.test.template.tool.Tool;
import com.test.template.tool.VelocityConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.junit.Test;

import java.io.StringWriter;

@Slf4j
public class ContributeETC {    //poName,工具tool，packageName,domainName
//    private ControllerConfig getCtConfig() {
//        ControllerConfig config = new ControllerConfig();
//        config.setDomainName("com.elecbook.content.model");
//        config.setTableName("contribute");
//        config.setPackageName("com.elecbook.content.api");
//        config.setControllerName(Tool.toUpperCase(config.getTableName(), 1) + "Controller");
//        return config;
//    }

    @Test
    public void generateCt() {
        VelocityEngine velocityEngine = VelocityConfig.getVelocityEngine();

        // 加载模板
        Template template = velocityEngine.getTemplate("publish/mp.controller.vm");

        // 设置上下文变量
        VelocityContext context = new VelocityContext();

        // 工具类
        ControllerConfig config = ControllerConfigFactory.getInstance().getConfig("contribute");
        context.put("tool", new Tool());
        context.put("config", config);


        // 渲染模板
        StringWriter writer = new StringWriter();
        template.merge(context, writer);

        // 输出结果
        System.out.println(writer.toString());
        String gFilePath = "src/test/output/" + config.getFileName();
        VelocityConfig.output(writer, gFilePath);
    }

//    private static ServiceMapperConfig getPageMethodProperties() {
//        ServiceMapperConfig config = new ServiceMapperConfig();
//        config.setPackageName("com.elecbook.content.service");
//        config.setTableName("contribute");
//        config.setDomainName("com.elecbook.content.model");
//        return config;
//    }

    @Test
    public void generateServiceMapper() {
        VelocityEngine velocityEngine = VelocityConfig.getVelocityEngine();

        // 加载模板
        Template template = velocityEngine.getTemplate("publish/mp.ServiceMapper.vm");

        // 设置上下文变量
        VelocityContext context = new VelocityContext();

        // 工具类
        ServiceMapperConfig config = ServiceMapperConfigFactory.getInstance().getConfig("contribute");
        context.put("tool", new Tool());
        context.put("config", config);


        // 渲染模板
        StringWriter writer = new StringWriter();
        template.merge(context, writer);

        // 输出结果
        System.out.println(writer.toString());
        String gFilePath = "src/test/output/" + config.getFileName();
        log.info("输出位置：{}", gFilePath);
        VelocityConfig.output(writer, gFilePath);
    }


    /**
     * description 自动生成js的api接口
     *
     * @author zken
     * @CreateDate 2024/12/4 13:33:57
     */
    @Test
    public void apiGenerate() {
        VelocityEngine velocityEngine = VelocityConfig.getVelocityEngine();

        // 加载模板
        Template template = velocityEngine.getTemplate("publish/js.api.vm");

        // 设置上下文变量
        VelocityContext context = new VelocityContext();

        // 工具类
        ApiConfig config = ApiConfigFactory.getInstance().getConfig("contribute");
        context.put("config", config);
        context.put("tool", new Tool());


        // 渲染模板
        StringWriter writer = new StringWriter();
        template.merge(context, writer);

        // 输出结果
        System.out.println(writer.toString());
        String gFilePath = "src/test/output/" + config.getFileName() + ".js";

        log.info("输出位置：{}", gFilePath);

        VelocityConfig.output(writer, gFilePath);
    }

    @Test
    public void xmlSubmitGenerate() {
        VelocityEngine velocityEngine = VelocityConfig.getVelocityEngine();

        // 加载模板
        Template template = velocityEngine.getTemplate("publish/vue.submit.entity.vm");

        // 设置上下文变量
        VelocityContext context = new VelocityContext();

        // 工具类
        XmlSubmitConfig config = XmlSubmitConfigFactory.getInstance().getConfig("contribute");
        context.put("config", config);
        context.put("apiConfig", ApiConfigFactory.getInstance().getConfig("contribute"));
        context.put("tool", new Tool());


        // 渲染模板
        StringWriter writer = new StringWriter();
        template.merge(context, writer);

        // 输出结果
        System.out.println(writer.toString());
        String gFilePath = "src/test/output/" + config.getFileName();
        log.info("output：{}", gFilePath);
        VelocityConfig.output(writer, gFilePath);
    }


    /**
     * 生成类型
     */
    @Test
    public void jsClassGenerate() {
        VelocityEngine velocityEngine = VelocityConfig.getVelocityEngine();

        // 加载模板
        Template template = velocityEngine.getTemplate("publish/js.entity.class.vm");

        // 设置上下文变量
        VelocityContext context = new VelocityContext();

        // 工具类
        JsClassConfig config = JsClassConfigFactory.getInstance().getConfig("contribute");
        context.put("config", config);
        context.put("tool", new Tool());


        // 渲染模板
        StringWriter writer = new StringWriter();
        template.merge(context, writer);

        // 输出结果
        System.out.println(writer.toString());
        String gFilePath = "src/test/output/" + config.getFileName();
        log.info("output：{}", gFilePath);
        VelocityConfig.output(writer, gFilePath);
    }


    @Test
    public void xmlGenerate() {
        VelocityEngine velocityEngine = VelocityConfig.getVelocityEngine();

        // 加载模板
        Template template = velocityEngine.getTemplate("hello-vue-submit.vm");

        // 设置上下文变量
        VelocityContext context = new VelocityContext();

        // 工具类
        XmlSubmitConfig config = XmlSubmitConfigFactory.getInstance().getConfig("contribute");
        context.put("config", config);
        context.put("apiConfig", ApiConfigFactory.getInstance().getConfig("contribute"));
        context.put("tool", new Tool());


        // 渲染模板
        StringWriter writer = new StringWriter();
        template.merge(context, writer);

        // 输出结果
        System.out.println(writer.toString());
        String gFilePath = "src/test/output/" + config.getFileName();
        log.info("output：{}", gFilePath);
        VelocityConfig.output(writer, gFilePath);
    }
}
